using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Mpi.Common;

namespace Xid.Controls.ParameterEditCtrls
{
    public partial class TimePickComboCtrl : UserControl
    {

        private string _endLabel = "End";
        private string _startLabel;
        public string StartLabel
        {
            get { return _startLabel; }
        }


        public string EndLabel
        {
            get { return _endLabel; }
            set { _endLabel = value; SetLabel(); }
        }

        private bool selected = false;

        public bool Selected
        {
            get { return selected; }
            set
            {
                if (selected != value)
                {
                    selected = value;
                    if (!selected)
                        SetViewMode(false);
                    else
                    {
                        //this.kryptonButtonTime.Focus();

                        if (this.GotSelected != null)
                            this.GotSelected(this, new EventArgs());
                    }
                }
            }
        }

        private bool _activated;

        public bool Activated
        {
            get { return _activated; }
            set 
            { 
                _activated = value;
                if (_activated)
                {
                    this.kryptonButtonTime.StateCommon.Back.Color1 = _orginalBackColor;
                }
                else
                {
                    this.kryptonButtonTime.StateCommon.Back.Color1 = Color.Gray;
                }
            }
        }


        public bool ShowExtendButton
        {
            get
            {
                return this.kryptonButtonAdd.Visible;
            }
            set
            {
                this.kryptonButtonAdd.Visible = value;
            }
        }

        private TimeSpan oldVal;
        public TimeSpan Time
        {
            get { return timeSpanBox1.Value; }
            set { timeSpanBox1.Value = value; this.oldVal = value; }
        }

        //public TimeSpan MinTime
        //{
        //    get { return timeSpanPicker1.Min; }
        //    set { timeSpanPicker1.Min = value;  }
        //}

        //public TimeSpan MaxTime
        //{
        //    get { return timeSpanPicker1.Max; }
        //    set { timeSpanPicker1.Max = value;  }
        //}


        private Color _orginalBackColor;
        public TimePickComboCtrl()
        {
            InitializeComponent();
            this.timeSpanBox1.ValueChanged += new EventHandler(timeSpanPicker1_ValueChanged);
            SetViewMode(true);
            SetLabel();
            //this.timeSpanPicker1.Visible = false;
            //this.kryptonButtonTime.Text = timeSpanPicker1.ReadableValue;
            //this.timeSpanPicker1.Leave += new EventHandler(timeSpanPicker1_Leave);
            timeSpanBox1.KeyPress += new KeyPressEventHandler(timeSpanPicker1_KeyPress);

            _orginalBackColor = kryptonButtonTime.StateCommon.Back.Color1;

        }

        void timeSpanPicker1_KeyPress(object sender, KeyPressEventArgs e)
        {
            char s = e.KeyChar;
            if (e.KeyChar == '\r')
            {
                SetViewMode(true);
                e.Handled = true;
            }
            double x = char.GetNumericValue(e.KeyChar);
            string y = char.ConvertFromUtf32(27);
            if (e.KeyChar == y[0])
            {
                this.Time = this.oldVal;
                SetViewMode(true);
                e.Handled = true;
            }


        }

        void timeSpanPicker1_Leave(object sender, EventArgs e)
        {

           // SetViewMode();

        }

        void timeSpanPicker1_ValueChanged(object sender, EventArgs e)
        {
            SetLabel();
        }

        private void SetLabel()
        {
            _startLabel = timeSpanBox1.ReadableValue2;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(timeSpanBox1.ReadableValue2);
            sb.Append(" to");
            if(!string.IsNullOrEmpty(_endLabel))
                sb.AppendFormat(" {0}",_endLabel);
            kryptonButtonTime.Values.Text = sb.ToString();

        }

        private void kryptonButtonTime_Click(object sender, EventArgs e)
        {
            if (this.selected)
                SetEditMode();
            else
            {
                this.Selected = true;
                
                
            }
        }

        private bool editMode = false;
        internal void SetViewMode(bool getFocus)
        {
            if (editMode)
            {
                this.SuspendLayout();
                this.panel1.Visible = false;
                this.kryptonButtonTime.Visible = true;
                this.kryptonButtonDropMe.Visible = false;
                this.ResumeLayout();
                OnTimeChanged();
                this.oldVal = this.Time;
                //if(getFocus)
                //    this.kryptonButtonTime.Focus(); //TODO: I believe it is not neede anymore.
                editMode = false;
            }
        }

        private void SetEditMode()
        {
            if (!editMode)
            {
                this.oldVal = this.Time;
                this.SuspendLayout();
                this.panel1.Visible = true;
                this.kryptonButtonTime.Visible = false;
                this.kryptonButtonDropMe.Visible = true;
                this.ResumeLayout();
                this.timeSpanBox1.Focus();
                editMode = true;
            }
        }



        private void OnTimeChanged()
        {
            if (this.oldVal != this.Time && this.TimeChanged != null)
            {
                TimeChanged(this, new PropertyHasChangedEventArgs<TimeSpan>(this.oldVal, this.Time));
            }
        }

        private void kryptonButtonDropMe_Click(object sender, EventArgs e)
        {
            if (this.DropMe != null)
                this.DropMe(this, new EventArgs());
            
        }

        private void kryptonButtonAdd_Click(object sender, EventArgs e)
        {
            if (this.AddPoint != null)
                this.AddPoint(this, new EventArgs());
        }

        public event EventHandler GotSelected;

        public event EventHandler DropMe;

        public event EventHandler AddPoint;

        public event EventHandler<PropertyHasChangedEventArgs<TimeSpan>> TimeChanged;


       
    }
}
